package com.example.demo.param.controller;

import com.example.demo.account.entity.SecurityUser;
import com.example.demo.common.utils.Constant;
import com.example.demo.common.utils.SpringSecurityUtils;
import com.example.demo.information.entity.School;
import com.example.demo.information.service.SchoolService;
import com.example.demo.param.entity.Param;
import com.example.demo.param.service.ParamService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
public class ParamController {

    @Autowired
    private SchoolService schoolService;
    @Autowired
    private ParamService paramService;

    @RequestMapping("/toParamPhoto")
    public String toParamPhoto(){
        return "param/paramPhotoList";
    }

    @RequestMapping("/paramPhotoList")
    @ResponseBody
    public Map<String,Object> paramPhotoList(String searchVal,Integer page,Integer limit){
        Map<String ,Object> map = new HashMap<>();//用于传回页面参数
        List<Map<String,Object>> list = new ArrayList<>();//记录拼接页面的list
        List<School> schoolList = new ArrayList<>();
        page = (page-1)*limit;
        int count = 0;
        if(StringUtils.isBlank(searchVal)){
            schoolList = schoolService.findPageAll(page,limit);
            count = schoolService.countAll();
        }else{
            schoolList = schoolService.findPageByNameLike("%"+searchVal+"%",page,limit);
            count = schoolService.countByNameLike("%"+searchVal+"%");
        }
        list = paramService.loadSchoolPhotoParamJson(schoolList);
        map.put("data",list);
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        return map;
    }

    @RequestMapping("/toPhotoParamAdd")
    public String toPhotoParamAdd(ModelMap map){
        Map<String,String> tempSpidMap = new LinkedHashMap<>();
        for(Map.Entry<String,String> entry:Constant.photoParamSpidMap.entrySet()){
            String spidStr = entry.getValue();
            String[] split = spidStr.split("@@");
            spidStr = split[0]+"("+split[1]+")";
            tempSpidMap.put(entry.getKey(),spidStr);
        }
        map.put("spidList",tempSpidMap);
        map.put("bkColorList",Constant.photoParamBkColorMap);
        Param param = paramService.findByPzsmAndXxxxIdNull();
        map.put("photoParam",param);
        map.put("photoParamSpid",param.getPzz());
        return "param/paramPhotoAdd";
    }

    private void savePhotoParam(Long xxId,String spid,String bkColor,String xgr){
        Param param = null;
        if(xxId==null){
            param = paramService.findByPzsmAndXxxxIdNull();
        }else{
            param = paramService.findSchoolPhotoParam(xxId);
        }
        paramService.savePhotoParam(param,xxId,spid,bkColor,xgr);
    }

    @RequestMapping("/photoParamAdd")
    @ResponseBody
    public String photoParamAdd(String xxId,String spid,String bkColor){
        SecurityUser currentUserDetails = SpringSecurityUtils.getCurrentUserDetails();
        Long xxIdL = null;
        if(StringUtils.isNotBlank(xxId)&&xxId.lastIndexOf(",")!=-1){
            String[] split = xxId.split(",");
            int count = 0;
            for(String s:split){
                xxIdL = null;
                xxIdL = StringUtils.isBlank(s)?null:Long.valueOf(s);
                if(xxIdL!=null){
                    try{
                        savePhotoParam(xxIdL,spid,bkColor,currentUserDetails.getName());
                        count++;
                    } catch (Exception e) {
                        e.printStackTrace();
                        continue;
                    }
                }
            }
            return "success@@成功操作"+count+"条数据.";
        }else{
            xxIdL = StringUtils.isBlank(xxId)?null:Long.valueOf(xxId);
            savePhotoParam(xxIdL,spid,bkColor,currentUserDetails.getName());
        }
        return "success";
    }

    @RequestMapping("/resetPhotoParam")
    @ResponseBody
    public String resetPhotoParam(String xxId){
        SecurityUser currentUserDetails = SpringSecurityUtils.getCurrentUserDetails();
        Long xxIdL = null;
        if(StringUtils.isNotBlank(xxId)&&xxId.lastIndexOf(",")!=-1){
            String[] split = xxId.split(",");
            int count = 0;
            for(String s:split){
                xxIdL = null;
                xxIdL = StringUtils.isBlank(s)?null:Long.valueOf(s);
                if(xxIdL!=null){
                    try{
                        savePhotoParam(xxIdL,Constant.defaultPhotoParamSpid,Constant.defaultPhotoParamBkColor
                                ,currentUserDetails.getName());
                        count++;
                    } catch (Exception e) {
                        e.printStackTrace();
                        continue;
                    }
                }
            }
            return "success@@成功操作"+count+"条数据.";
        }else{
            xxIdL = StringUtils.isBlank(xxId)?null:Long.valueOf(xxId);
            if(xxIdL!=null){
                savePhotoParam(xxIdL,Constant.defaultPhotoParamSpid,Constant.defaultPhotoParamBkColor
                        ,currentUserDetails.getName());
                return "success";
            }else{
                return "未选择学校";
            }
        }
    }

}
